﻿using System;
using System.Collections.Generic;
using OfficeOpenXml.ConditionalFormatting;
using UnityEngine;
using UnityEngine.Video;

public enum MoveDir
{
    None,
    Down,
    Up,
    Left,
    Right
}

public class PlayerEntity : MonoBehaviour
{

    public float fMoveSpeed = 0.2f;

    private bool bMove = false;

    private MoveDir moveDir = MoveDir.None;

    private Animator frameAnimation;

    private float fTargetX = 0;
    private float fTarGetY = 0;

    private int iTargetX = 0;
    private int iTargetY = 0;
    private CameraFollow follow;

    private int iNum = 0;

    public int DebugX = 0;
    public int DebugY = 0;
    void Awake()
    {
        frameAnimation = GetComponent<Animator>();

        follow = GameObject.Find("Main Camera").GetComponent<CameraFollow>();

        Vector2 vector2 = MapManager.Instance.GetPlayerCell();

        iTargetX = (int)vector2.x;
        iTargetY = (int)vector2.y;

        transform.localPosition = new Vector3(iTargetX * 0.8f, 0, iTargetY * 0.8f);
        follow.SetPlayer(this);
    }

    public void MoveStop()
    {
        moveDir = MoveDir.None;
        bMove = false;
        frameAnimation.SetBool("walk", false);
        fTarGetY = 0;
        fTargetX = 0;

        if (MapManager.Instance.GetChip(iTargetX, iTargetY))
        {
            iNum++;

            if (iNum >= 3)
            {
                HPManager.Instance.OnGameOver();
            }
        }
    }
    [ContextMenu("DebugMove")]
    public void DebugMove()
    {
        iTargetX = DebugX;
        iTargetY = DebugY;
        transform.localPosition = new Vector3(DebugX * 0.8f, 0, DebugY * 0.8f);
    }

    public void Move(MoveDir dir)
    {
        if (moveDir == MoveDir.Right)
        {
            if (dir != MoveDir.Left)
            {
                return;
            }
        }

        if (moveDir == MoveDir.Left)
        {
            if(dir != MoveDir.Right) return;
        }

        if (moveDir == MoveDir.Down)
        {
            if(dir!= MoveDir.Up)return;
        }

        if (moveDir == MoveDir.Up)
        {
            if(dir != MoveDir.Down) return;
        }

        int tmpTargetX = iTargetX;
        int tmpTargetY = iTargetY;

        switch (dir)
        {
            case MoveDir.Down:
                iTargetY--;
                break;
            case MoveDir.Up:
                iTargetY++;
                break;
            case MoveDir.Right:
                iTargetX++;
                break;
            case MoveDir.Left:
                iTargetX--;
                break;
        }
        float angle = GetAngle(dir);
        transform.localEulerAngles = new Vector3(90, angle, 0);

        if (iTargetX < 0)
        {
            iTargetX = 0;
            return;
        }

        if (iTargetX > MapManager.Instance.GetMaxX() - 1)
        {
            iTargetX = (int) MapManager.Instance.GetMaxX() - 1;
            return;
        }

        if (iTargetY < 0)
        {
            iTargetY = 0;
            return;
        }

        if (iTargetY > MapManager.Instance.GetMaxY() - 1)
        {
            iTargetY = (int) MapManager.Instance.GetMaxY() - 1;
            return;
        }

        if (MapManager.Instance.IsWall(iTargetX, iTargetY))
        {
            iTargetX = tmpTargetX;
            iTargetY = tmpTargetY;
            return;
        }

        frameAnimation.SetBool("walk", true);
        fTargetX = MapManager.Instance.GetXToPos(iTargetX);
        fTarGetY = MapManager.Instance.GetYToPos(iTargetY);

        this.moveDir = dir;
        bMove = true;
    }

    private float GetAngle(MoveDir dir)
    {
        float changeAngle = 0;

        switch (dir)
        {
            case MoveDir.Down:
                changeAngle = 180;
                break;
            case MoveDir.Left:
                changeAngle = -90;
                break;
            case MoveDir.Right:
                changeAngle = 90;

                break;
            case MoveDir.Up:
                changeAngle = 0;
                break;
        }

        return changeAngle;
    }

    void Update()
    {
        if(!bMove) return;

        float moveX = transform.localPosition.x;
        float moveY = transform.localPosition.z;

        switch (moveDir)
        {
            case MoveDir.Down:
                moveY -= Time.deltaTime * fMoveSpeed;

                if (moveY <= fTarGetY)
                {
                    moveY = fTarGetY;
                }
                break;
            case MoveDir.Up:
                moveY += Time.deltaTime * fMoveSpeed;

                if (moveY >= fTarGetY)
                {
                    moveY = fTarGetY;
                }
                break;
            case MoveDir.Left:
                moveX -= Time.deltaTime * fMoveSpeed;

                if (moveX <= fTargetX)
                {
                    moveX = fTargetX;
                }
                break;
            case MoveDir.Right:
                moveX += Time.deltaTime * fMoveSpeed;

                if (moveX >= fTargetX)
                {
                    moveX = fTargetX;
                }
                break;
        }
        transform.localPosition = new Vector3(moveX, 0, moveY);
        if (moveX == fTargetX && moveY == fTarGetY)
        {
            MoveStop();
        }
        

        //bool bIsIsOut = MapManager.Instance.IsOut(moveX, moveY);

        //if (!bIsIsOut)
        //{
        //    bool bCanMove = MapManager.Instance.IsMove(moveX, moveY, moveDir);
        //    if (bCanMove)
        //    {
        //        transform.localPosition = new Vector3(moveX, 0, moveY);
        //    }
        //}
    }
}
